package com.dyc.caradvertisting.ui.fmt.pay;


import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.event.StartBrotherEvent;
import com.dyc.caradvertisting.ui.fmt.pay.authen.AuthenFmt;
import com.dyc.caradvertisting.ui.fmt.pay.tx.DepositFragment;
import com.dyc.caradvertisting.ui.fmt.tab.smsg.SystemMesFmt;
import com.njh.tools.base.BaseFragment;
import com.njh.tools.common.userInfo.SPUserInfo;
import com.sevenheaven.segmentcontrol.SegmentControl;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 资产
 */
public class AssetFmt extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.toolbar_title_tv)
    TextView toolbarTitleTv;
    @BindView(R.id.tv_mg)
    TextView tvMg;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_zic)
    TextView tvZic;
    @BindView(R.id.line_all)
    LinearLayout lineAll;
    @BindView(R.id.line_matter)
    LinearLayout lineMatter;
    @BindView(R.id.txt_message)
    TextView txtMessage;
    @BindView(R.id.line_serve)
    LinearLayout lineServe;
    @BindView(R.id.segment_control)
    SegmentControl segmentControl;

    public AssetFmt() {
        // Required empty public constructor
    }

    public static AssetFmt newInstance() {
        AssetFmt fragment = new AssetFmt();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return getView(inflater, container);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_asset_fmt;
    }

    @Override
    public void initView(View baseView) {
        ButterKnife.bind(this, baseView);
        toolbarTitleTv.setText(getString(R.string.title_my_asset));
        toolbar.setNavigationIcon(com.njh.tools.R.mipmap.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _mActivity.onBackPressed();
            }
        });
    }

    PopupWindow popupWindow;


    @Override
    public boolean isSwipeBack() {
        return true;
    }

    @Override
    public void initPresenter() {

    }

    @OnClick({R.id.tv_mg, R.id.txt_message})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mx:
                popupWindow.dismiss();
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(PayDetailFmt.newInstance()));
                break;
            case R.id.tv_yhc:
                popupWindow.dismiss();
                break;
            case  R.id.tv_cz:
                popupWindow.dismiss();
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(PayFmt.newInstance()));
                break;
            case R.id.tv_tx:
                popupWindow.dismiss();
                if (TextUtils.isEmpty(SPUserInfo.getAutonym(this.getContext()))){
                    // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                    EventBus.getDefault().post(new StartBrotherEvent(DepositFragment.newInstance()));
                }else {
                    // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                    EventBus.getDefault().post(new StartBrotherEvent(AuthenFmt.newInstance()));
                }
                break;
            case R.id.tv_mg:
                if (null==popupWindow) {
                    popupWindow = new PopupWindow(this.getContext());
                    View pview = LayoutInflater.from(this.getContext()).inflate(R.layout.layout_popu, null);
                    pview.findViewById(R.id.tv_mx).setOnClickListener(this);
                    pview.findViewById(R.id.tv_yhc).setOnClickListener(this);
                    pview.findViewById(R.id.tv_cz).setOnClickListener(this);
                    pview.findViewById(R.id.tv_tx).setOnClickListener(this);
                    popupWindow.setContentView(pview);
                    popupWindow.setWidth(400);
                    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                    // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                    // 我觉得这里是API的一个bug
                    popupWindow.setBackgroundDrawable(new BitmapDrawable());
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.setFocusable(true);
                    popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
                }
                if (!popupWindow.isShowing()){
                    // 设置好参数之后再show
                    popupWindow.showAsDropDown(view);
                }else {
                    popupWindow.dismiss();
                }
                break;
            case R.id.txt_message:
                break;
        }
    }
}
